package com.atguigu.distributed.lock.leecode;

import java.util.HashMap;
import java.util.Map;

/**
 * 力扣中等3：无重复字符的最长子串
 */
public class NorepeatedMaxString {
    public int lengthOfLongestSubstring2(String s) {
        Map<Character, Integer> map = new HashMap<>();
        int ans = 0, left = 0;
        int i;
        for (i = 0; i < s.length(); i++) {
            char cur = s.charAt(i);
            if (map.containsKey(cur)) {
                ans = Math.max(ans, i - left);
                left = Math.max(left, map.get(cur) + 1);
            }
            map.put(cur, i);
        }
        return Math.max(ans,i-left);
    }


    public int lengthOfLongestSubstring(String s) {
        Map<Character, Integer> map = new HashMap<>();
        int ans = 0, left = 0;
        int i;
        for (i = 0; i < s.length(); i++) {
            char cur = s.charAt(i);
            if (map.containsKey(cur)) {
                ans = Math.max(ans, i - left);
                left = Math.max(left, map.get(cur) + 1);
            }
            map.put(cur, i);
        }
        return Math.max(ans, i - left);
    }
}
